package com.atguigu.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioFileChannel03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("1.txt");
        FileChannel channel01 = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel channel02 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        while(true){
            //循环读取 写入到buffer中
            int read = channel01.read(byteBuffer);
            if(read==-1){
                break;
            }else{
                byteBuffer.flip();
                //将buffer中的数据写入到channel中
                channel02.write(byteBuffer);
                byteBuffer.clear();//标志位重置
            }
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
